<?php
class ControllerAffiliateEdit extends FrontController {

	public function index() {
		if (!$this->affiliate->isLogged()) {
			$this->session->data['redirect'] = US('affiliate/edit');
			$this->redirect(US('affiliate/login'));
		}

		$this->document->setTitle($this->_['heading_title']);
		$this->document->addScript('static/javascript/jquery/jquery.validate.js');
		
		M('affiliate/affiliate');

		if ($this->request->isPost() && $this->validate()) {
			$this->model_affiliate_affiliate->editAffiliate($this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(U('affiliate/account'));
		}

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_account'],
			'href'      => US('affiliate/account'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_edit'],
			'href'      => US('affiliate/edit'),
        	'separator' => $this->_['text_separator']
      	);

		if (!$this->request->isPost()) {
			$affiliate_info = $this->model_affiliate_affiliate->getAffiliate($this->affiliate->getId());
		}

		if (!empty($affiliate_info)) {
			$this->data['firstname'] = $affiliate_info['firstname'];
			$this->data['lastname'] = $affiliate_info['lastname'];
			$this->data['email'] = $affiliate_info['email'];
			$this->data['telephone'] = $affiliate_info['telephone'];
			$this->data['fax'] = $affiliate_info['fax'];
			$this->data['company'] = $affiliate_info['company'];
			$this->data['website'] = $affiliate_info['website'];
			$this->data['address_1'] = $affiliate_info['address_1'];
			$this->data['address_2'] = $affiliate_info['address_2'];
			$this->data['postcode'] = $affiliate_info['postcode'];
			$this->data['city'] = $affiliate_info['city'];
			$this->data['country_id'] = $affiliate_info['country_id'];
			$this->data['zone_id'] = $affiliate_info['zone_id'];
		} else {
			$this->data['firstname'] = P('firstname');
			$this->data['lastname'] = P('lastname');
			$this->data['email'] = P('email');
			$this->data['telephone'] = P('telephone');
			$this->data['fax'] = P('fax');
			$this->data['company'] = P('company');
			$this->data['website'] = P('website');
			$this->data['address_1'] = P('address_1');
			$this->data['address_2'] = P('address_2');
			$this->data['postcode'] = P('postcode');
			$this->data['city'] = P('city');
			$this->data['country_id'] = P('country_id');
			$this->data['zone_id'] = P('zone_id');
		}

    	$this->data['countries'] = cache_read('country.php');

		$this->display('affiliate/edit.tpl');
	}

	protected function validate() {
		if (($this->affiliate->getEmail() != $this->request->post['email']) && $this->model_affiliate_affiliate->getTotalAffiliatesByEmail($this->request->post['email'])) {
			$this->setMessage('error_warning', $this->_['error_exists']);
			return false;
		}
		
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('firstname', '', 'required|range_length[1,32]', $this->_['error_firstname']);		
		$this->form_validation->set_rules('lastname', '', 'required|range_length[1,32]', $this->_['error_lastname']);
		
		$this->form_validation->set_rules('email', '', 'required|range_length[5,96]|email', $this->_['error_email']);
		
		$this->form_validation->set_rules('telephone', '', 'required|range_length[3,32]', $this->_['error_telephone']);
		
		$this->form_validation->set_rules('address_1', '', 'required|range_length[3,128]', $this->_['error_address_1']);
		
		$this->form_validation->set_rules('city', '', 'required|range_length[2,128]', $this->_['error_city']);
		
		

		M('localisation/country');
		$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);

		if ($country_info && $country_info['postcode_required']) {
			$this->form_validation->set_rules('postcode', '', 'required|range_length[2,10]', $this->_['error_postcode']);	
		}
		
		$this->form_validation->set_rules('country_id', '', 'required', $this->_['error_country']);
		$this->form_validation->set_rules('zone_id', '', 'required', $this->_['error_zone']);

		return $this->form_validation->run();
	}
}
?>